//
//  ChangeNameViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-5-30.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "ChangeNameViewController.h"
#import "NSString+ContainChinese.h"
#import "NYMemberCenterDataManager.h"
#import "JSON.h"

@interface ChangeNameViewController ()

@end

@implementation ChangeNameViewController

@synthesize nameTextField;

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveButtonPressed:(id)sender {
    [self.nameTextField resignFirstResponder];
    NSString *validString = [self checkValid];
    if(validString)
    {
        [[RYHUDManager sharedManager] showWithMessage:validString customView:nil hideDelay:2.f];
    }
    else
    {
        NSString *suburl = [NSString stringWithFormat:kChangeNameUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,nameTextField.text];
        suburl = [suburl stringByAppendingString:@"app000010"];
        NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:nil];
        [[RYHUDManager sharedManager] startedNetWorkActivityWithText:@"用户名修改中..."];
    }
}

- (NSString *)checkValid
{
    //username长度1-50 不为中文
    if(self.nameTextField.text.length < 1)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"用户名"];
    else if(self.nameTextField.text.length > 50)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000002E"],@"用户名"];
    else if([NSString containChinese:self.nameTextField.text])
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000003E"],@"用户名"];
    else if([self.nameTextField.text rangeOfString:@"'"].length > 0)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000003E"],@"用户名"];
    return nil;
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:@"用户名"];
    [self setLeftNaviItemWithTitle:@"返回" imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        self.view = nil;
        self.nameTextField = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    self.nameTextField = nil;
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [nameTextField release];
    [super dealloc];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];
    NSDictionary *dict = [str JSONValue];
    if([[dict objectForKey:@"result"] isEqualToString:@"1"])
    {
        [NYMemberCenterDataManager sharedManager].loginMember.userName = self.nameTextField.text;
        [[NSNotificationCenter defaultCenter] postNotificationName:kChangeNameSucceedNotification object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
    [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
}
- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}

@end
